﻿using System;
using System.Collections.Generic;

namespace NodeJS
{
    using NodeJS.stream;
    /// <summary>
    /// The process object is a global object and can be accessed from anywhere. It is an instance of EventEmitter.
    /// </summary>
    public abstract class Process : EventEmitter
    {
        /// <summary>
        /// Emitted when the process is about to exit. This is a good hook to perform constant time checks of the module's state (like for unit tests).
        /// The main event loop will no longer be run after the 'exit' callback finishes, so timers may not be scheduled.
        /// </summary>
        [EvalAtCompile(Value = "exit")]
        public const NodeEvent EXIT = null;

        /// <summary>
        /// Emitted when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur.
        /// Note that uncaughtException is a very crude mechanism for exception handling and may be removed in the future.
        /// Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!
        /// Do not use it as the node.js equivalent of On Error Resume Next. An unhandled exception means your application - and by extension node.js itself - is in an undefined state. Blindly resuming means anything could happen.
        /// </summary>
        [EvalAtCompile(Value = "uncaughtException")]
        public const NodeEvent<Exception> UNCAUGHT_EXCEPTION = null;


        /// <summary>
        /// A Writable Stream to stdout.
        /// process.stderr and process.stdout are unlike other streams in Node in that writes to them are usually blocking. They are blocking in the case that they refer to regular files or TTY file descriptors. In the case they refer to pipes, they are non-blocking like other streams.
        /// </summary>
        public readonly Writable Stdout;//TODO:isTTY, TTY Module

        /// <summary>
        /// A writable stream to stderr.
        /// process.stderr and process.stdout are unlike other streams in Node in that writes to them are usually blocking. They are blocking in the case that they refer to regular files or TTY file descriptors. In the case they refer to pipes, they are non-blocking like other streams.
        /// </summary>
        public readonly Writable Stderr;//TODO:isTTY, TTY Module

        /// <summary>
        /// A Readable Stream for stdin. The stdin stream is paused by default, so one must call process.stdin.resume() to read from it.
        /// </summary>
        public readonly Readable Stdin;//TODO:isTTY, TTY Module

        /// <summary>
        /// An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.
        /// </summary>
        public readonly string[] Argv;

        /// <summary>
        /// This is the absolute pathname of the executable that started the process.
        /// </summary>
        public readonly string ExecPath;

        /// <summary>
        /// This is the set of node-specific command line options from the executable that started the process. These options do not show up in process.argv, and do not include the node executable, the name of the script, or any options following the script name. These options are useful in order to spawn child processes with the same execution environment as the parent.
        /// </summary>
        public readonly string[] ExecArgv;

        /// <summary>
        /// This causes node to emit an abort. This will cause node to exit and generate a core file.
        /// </summary>
        public abstract void Abort();

        /// <summary>
        /// Changes the current working directory of the process or throws an exception if that fails.
        /// </summary>
        /// <param name="directory"></param>
        public abstract void Chdir(string directory);

        /// <summary>
        /// Returns the current working directory of the process.
        /// </summary>
        /// <returns></returns>
        public abstract string Cwd();

        /// <summary>
        /// An object containing the user environment. See environ(7).
        /// </summary>
        public readonly object/*TODO*/ Env;

        /// <summary>
        /// Ends the process with the specified code. If omitted, exit uses the 'success' code 0.
        /// </summary>
        /// <param name="code"></param>
        public abstract void Exit(int code = 0);

        /// <summary>
        /// Gets the group identity of the process. This is the numerical group id, not the group name.
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// </summary>
        /// <returns></returns>
        public abstract int Getgid();

        /// <summary>
        /// Sets the group identity of the process. This accepts either a numerical ID or a groupname string. If a groupname is specified, this method blocks while resolving it to a numerical ID.
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// </summary>
        /// <param name="id"></param>
        public abstract void Setgid(int id);

        /// <summary>
        /// /// Sets the group identity of the process. This accepts either a numerical ID or a groupname string. If a groupname is specified, this method blocks while resolving it to a numerical ID.
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// </summary>
        /// <param name="id"></param>
        public abstract void Setgid(string id);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Gets the user identity of the process. This is the numerical userid, not the username.
        /// </summary>
        /// <returns></returns>
        public abstract int Getuid();

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Sets the user identity of the process. This accepts either a numerical ID or a username string. If a username is specified, this method blocks while resolving it to a numerical ID.
        /// </summary>
        /// <param name="id"></param>
        public abstract void Setuid(int id);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Sets the user identity of the process. This accepts either a numerical ID or a username string. If a username is specified, this method blocks while resolving it to a numerical ID.
        /// </summary>
        /// <param name="id"></param>
        public abstract void Setuid(string id);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// </summary>
        /// <returns>Returns an array with the supplementary group IDs. POSIX leaves it unspecified if the effective group ID is included but node.js ensures it always is.</returns>
        public abstract int[] Getgroups();

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Sets the supplementary group IDs. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// The list can contain group IDs, group names or both.
        /// </summary>
        /// <param name="groups"></param>
        public abstract void Setgroups(int[] groups);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Sets the supplementary group IDs. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// The list can contain group IDs, group names or both.
        /// </summary>
        /// <param name="groups"></param>
        public abstract void Setgroups(string[] groups);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Sets the supplementary group IDs. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// The list can contain group IDs, group names or both.
        /// </summary>
        /// <param name="groups"></param>
        public abstract void Setgroups(object[] groups);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Reads /etc/group and initializes the group access list, using all groups of which the user is a member. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// user is a user name or user ID. extra_group is a group name or group ID.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="extra_group"></param>
        public abstract void Initgroups(int user, int extra_group);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Reads /etc/group and initializes the group access list, using all groups of which the user is a member. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// user is a user name or user ID. extra_group is a group name or group ID.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="extra_group"></param>
        public abstract void Initgroups(int user, string extra_group);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Reads /etc/group and initializes the group access list, using all groups of which the user is a member. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// user is a user name or user ID. extra_group is a group name or group ID.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="extra_group"></param>
        public abstract void Initgroups(string user, int extra_group);

        /// <summary>
        /// Note: this function is only available on POSIX platforms (i.e. not Windows)
        /// Reads /etc/group and initializes the group access list, using all groups of which the user is a member. This is a privileged operation, meaning you need to be root or have the CAP_SETGID capability.
        /// user is a user name or user ID. extra_group is a group name or group ID.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="extra_group"></param>
        public abstract void Initgroups(string user, string extra_group);

        /// <summary>
        /// A compiled-in property that exposes NODE_VERSION.
        /// </summary>
        public readonly string Version;

        /// <summary>
        /// A property exposing version strings of node and its dependencies.
        /// </summary>
        public readonly Dictionary<string, string> Versions;

        /// <summary>
        /// An Object containing the JavaScript representation of the configure options that were used to compile the current node executable. This is the same as the "config.gypi" file that was produced when running the ./configure script.
        /// </summary>
        public readonly object Config;

        /// <summary>
        /// Send a signal to a process.
        /// Note that just because the name of this function is process.kill, it is really just a signal sender, like the kill system call. The signal sent may do something other than kill the target process.
        /// </summary>
        /// <param name="pid">pid is the process id.</param>
        /// <param name="signal">signal is the string describing the signal to send.
        /// Signal names are strings like 'SIGINT' or 'SIGHUP'. If omitted, the signal will be 'SIGTERM'
        /// </param>
        public abstract void Kill(int pid, string signal = "");

        /// <summary>
        /// The PID of the process.
        /// </summary>
        public readonly int Pid;

        /// <summary>
        /// Getter/setter to set what is displayed in 'ps'.
        /// When used as a setter, the maximum length is platform-specific and probably short.
        /// On Linux and OS X, it's limited to the size of the binary name plus the length of the command line arguments because it overwrites the argv memory.
        /// </summary>
        public string Title;

        /// <summary>
        /// What processor architecture you're running on: 'arm', 'ia32', or 'x64'.
        /// </summary>
        public readonly string Arch;

        /// <summary>
        /// Returns an object describing the memory usage of the Node process measured in bytes.
        /// </summary>
        /// <returns>{ rss: 4935680,  heapTotal: 1826816,  heapUsed: 650472 }</returns>
        public abstract object/*TODO*/ MemoryUsage();


        /// <summary>
        /// On the next loop around the event loop call this callback. This is not a simple alias to setTimeout(fn, 0), it's much more efficient. 
        /// It typically runs before any other I/O events fire, but there are some exceptions. See process.maxTickDepth below.
        /// </summary>
        /// <param name="callback"></param>
        public abstract void NextTick(Action callback);

        /// <summary>
        /// Number Default = 1000
        /// Callbacks passed to process.nextTick will usually be called at the end of the current flow of execution, and are thus approximately as fast as calling a function synchronously. Left unchecked, this would starve the event loop, preventing any I/O from occurring.
        /// The process.maxTickDepth value is the maximum depth of nextTick-calling nextTick-callbacks that will be evaluated before allowing other forms of I/O to occur.
        /// </summary>
        public int MaxTickDepth;

        /// <summary>
        /// Sets the process's file mode creation mask. Child processes inherit the mask from the parent process.
        /// </summary>
        /// <param name="mask"></param>
        /// <returns> Returns the old mask if mask argument is given, otherwise returns the current mask.</returns>
        public abstract int Umask(int mask);

        /// <summary>
        /// Number of seconds Node has been running.
        /// </summary>
        /// <returns></returns>
        public abstract long Uptime();

        /// <summary>
        /// Returns the current high-resolution real time in a [seconds, nanoseconds] tuple Array. It is relative to an arbitrary time in the past. It is not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals.
        /// You may pass in the result of a previous call to process.hrtime() to get a diff reading, useful for benchmarks and measuring intervals:
        /// </summary>
        /// <returns></returns>
        /*TODO:[x,y] to replace lomg[]*/
        public abstract long[] Hrtime(long[] time = null);
    }
}
